package com.hqyj.finance.server.bill.financebill.utils;

import cn.hutool.crypto.digest.BCrypt;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class MyUtils {

    private static String MySalt; //我自己的盐；


    //检查是否是合法的手机号码
    public static boolean isMobileNumber(String mobileStr) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(19[0-9])|(147))\\d{8}$");
        Matcher m = p.matcher(mobileStr);
        return m.matches();
    }

    public static boolean isEmail(String email) {
        // return ReUtil.isMatch("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$",email);
        //正则表达式匹配任何字符
        String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
        //编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        //检索匹配器对象
        Matcher m = pattern.matcher(email);
        return m.matches();
    }

    @Value("${myproject.bcrypt-salt}")
    public void setSalt(String salt){
        MyUtils.MySalt = salt;
    }

    public static String encodePassword(String password){
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public static boolean checkPassword(String password,String encodePass){
        return BCrypt.checkpw(password,encodePass);
    }

    public static void uploadFileByQiniu(){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
         //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = "PNrmJGeje_2m9DUpaWqvwdQFjijIvdyr1ZnrN2Ok";
        String secretKey = "WHJF1UvKGpz9SpfMhhUpPIxE2U3C9c_JykDkz1Mn";
        String bucket = "shi-peng-qiniu";
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "D:\\uploadFiles\\欠条样例图片001.jpg";
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);
                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        }
    }
}
